﻿package {
	
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.DataEvent;
	import flash.events.Event;
	import flash.text.TextField;
	import flash.utils.getTimer;
	import meps.net.CLocalBridge;
	
	public class Main extends Sprite {
		
		private var _text1:TextField;
		private var _text2:TextField;
		private var _text3:TextField;
		private var _bridge1:CLocalBridge;
		private var _bridge2:CLocalBridge;
		private var _bridge3:CLocalBridge;
		private var _receiver1:Object = {
			onOne:onOneListener1,
			onTwo:onTwoListener1,
			onThree:onThreeListener1
		};
		private var _receiver2:Object = {
			onOne:onOneListener2,
			onTwo:onTwoListener2,
			onThree:onThreeListener2
		};
		private var _receiver3:Object = {
			onOne:onOneListener3,
			onTwo:onTwoListener3,
			onThree:onThreeListener3
		};
		
		public function Main():void {
			if (stage)
				init();
			else
				addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(e:Event = null):void {
			removeEventListener(Event.ADDED_TO_STAGE, init);
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			var width:int = stage.stageWidth;
			var height:int = stage.stageHeight / 3;
			
			_text1 = new TextField();
			_text1.background = true;
			_text1.backgroundColor = 0xDDFFDD;
			_text1.x = 0;
			_text1.y = 0;
			_text1.width = width;
			_text1.height = height;
			addChild(_text1);
			
			_text2 = new TextField();
			_text2.background = true;
			_text2.backgroundColor = 0xDDDDFF;
			_text2.x = 0;
			_text2.y = height;
			_text2.width = width;
			_text2.height = height;
			addChild(_text2);
			
			_text3 = new TextField();
			_text3.background = true;
			_text3.backgroundColor = 0xFFDDDD;
			_text3.x = 0;
			_text3.y = height * 2;
			_text3.width = width;
			_text3.height = height;
			addChild(_text3);
			
			log("init");
			
			_bridge1 = new CLocalBridge(_receiver1);
			_bridge1.addEventListener(CLocalBridge.CONNECT, onConnect);
			_bridge1.addEventListener(CLocalBridge.CALL, onCall);
			_bridge1.connect("localbridge.between");
			
			_bridge2 = new CLocalBridge(_receiver2);
			_bridge2.addEventListener(CLocalBridge.CONNECT, onConnect);
			_bridge2.addEventListener(CLocalBridge.CALL, onCall);
			_bridge2.connect("localbridge.loopback");
			
			_bridge3 = new CLocalBridge(_receiver3);
			_bridge3.addEventListener(CLocalBridge.CONNECT, onConnect);
			_bridge3.addEventListener(CLocalBridge.CALL, onCall);
			_bridge3.connect("localbridge.loopback");
			
			_bridge1.call("onOne", "3 from bridge one");
			_bridge2.call("onOne", "3 from second bridge");
			_bridge3.call("onOne", "3 from AS3");
			_bridge1.call("onTwo", 3234.5678, { a:"a", b:3, c:true }, ["a", 3, true]);
			_bridge2.call("onTwo", 3678.1234, { x:false, y:[3, 2, 3], z:"0" }, [3, "b", false]);
			_bridge3.call("onTwo", 3333.3333, { x:1, y:{}, z:3 }, [true, 3, "c"]);
			_bridge1.call("onThree", "a", 3, true);
			_bridge2.call("onThree", 3, "b", false);
			_bridge3.call("onThree", true, 3, "c");
			_bridge1.call("onNone");
			_bridge2.call("onNone", "boo");
			_bridge3.call("onNone", 3234);
		}
		
		private function log(text:String, num:int = -1):void {
			var time:String = String(getTimer());
			while (time.length < 4)
				time = "0" + time;
			time =  time.substr(0, time.length - 3) + "." + time.substr(-3);
			if (num == 1) {
				_text1.appendText(time + ": " + text + "\n");
				trace("1MN:" + text);
			} else if (num == 2) {
				_text2.appendText(time + ": " + text + "\n");
				trace("2MN:" + text);
			} else if (num == 3) {
				_text3.appendText(time + ": " + text + "\n");
				trace("3MN:" + text);
			} else {
				_text1.appendText(time + ": " + text + "\n");
				_text2.appendText(time + ": " + text + "\n");
				_text3.appendText(time + ": " + text + "\n");
				trace("MN:" + text);
			}
			_text1.scrollV = _text1.maxScrollV;
			_text2.scrollV = _text2.maxScrollV;
			_text3.scrollV = _text3.maxScrollV;
		}
		
		private function onConnect(event:Event):void {
			//log("onConnect", event.target == _bridge1 ? 1 : event.target == _bridge2 ? 2 : 3);
		}
		
		private function onCall(event:Event):void {
			//log("onCall", event.target == _bridge1 ? 1 : event.target == _bridge2 ? 2 : 3);
		}
		
		private function onOneListener1(param:String):void {
			log("onOneListener param:" + param, 1);
		}
		
		private function onTwoListener1(par1:Number, par2:Object, par3:Array):void {
			log("onTwoListener par1:" + par1 +", par2:" + strObj(par2) + ", par3:" + par3, 1);
		}
		
		private function onThreeListener1(... args:Array):void {
			log("onThreeListener args:" + args, 1);
		}
		
		private function onOneListener2(param:String):void {
			log("onOneListener param:" + param, 2);
		}
		
		private function onTwoListener2(par1:Number, par2:Object, par3:Array):void {
			log("onTwoListener par1:" + par1 +", par2:" + strObj(par2) + ", par3:" + par3, 2);
		}
		
		private function onThreeListener2(... args:Array):void {
			log("onThreeListener args:" + args, 2);
		}
		
		private function onOneListener3(param:String):void {
			log("onOneListener param:" + param, 3);
		}
		
		private function onTwoListener3(par1:Number, par2:Object, par3:Array):void {
			log("onTwoListener par1:" + par1 +", par2:" + strObj(par2) + ", par3:" + par3, 3);
		}
		
		private function onThreeListener3(... args:Array):void {
			log("onThreeListener args:" + args, 3);
		}
		
		private function strObj(o:Object):String {
			var s:String = "";
			for (var i:String in o)
				s += (s.length > 0 ? ", " : " ") + i + ":" + o[i];
			return "{" + s + " }";
		}
		
	}
	
}
